package com.pennywise.mapper;

import com.pennywise.model.Dictionary;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DictionaryMapper {
    
    @Insert("""
        INSERT INTO dictionary (
            type, code, name, sort, enabled, remark, create_time, update_time
        ) VALUES (
            #{type}, #{code}, #{name}, #{sort}, #{enabled}, #{remark}, #{createTime}, #{updateTime}
        )
    """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Dictionary dictionary);
    
    @Update("""
        UPDATE dictionary SET
            type = #{type},
            code = #{code},
            name = #{name},
            sort = #{sort},
            enabled = #{enabled},
            remark = #{remark},
            update_time = #{updateTime}
        WHERE id = #{id}
    """)
    void update(Dictionary dictionary);
    
    @Delete("DELETE FROM dictionary WHERE id = #{id}")
    void deleteById(Long id);
    
    @Select("SELECT * FROM dictionary WHERE id = #{id}")
    Dictionary findById(Long id);
    
    @Select("SELECT * FROM dictionary WHERE type = #{type} AND enabled = true ORDER BY sort")
    List<Dictionary> findByType(String type);
    
    @Select("""
        SELECT * FROM dictionary 
        WHERE type = #{type} 
        AND enabled = true 
        AND code = #{code}
        LIMIT 1
    """)
    Dictionary findByTypeAndCode(String type, String code);
    
    @Select("""
        SELECT * FROM dictionary 
        WHERE type = #{type} 
        <if test="keyword != null and keyword != ''">
        AND (code LIKE #{keyword} OR name LIKE #{keyword})
        </if>
        ORDER BY sort
    """)
    List<Dictionary> search(@Param("type") String type, @Param("keyword") String keyword);
} 